package nl.zoidberg.calculon.analyzer;

import java.util.List;
import java.util.Map;

import nl.zoidberg.calculon.engine.BishopMoveGenerator;
import nl.zoidberg.calculon.model.Board;
import nl.zoidberg.calculon.model.Piece;

public class BishopMobilityScorer implements PositionScorer {

	public float scorePosition(Board board, Map<Byte, List<int[]>> pieceMap) {
		float score = 0;
		score += getScore(board, Piece.WHITE, pieceMap);
		score -= getScore(board, Piece.BLACK, pieceMap);
		return score;
	}

	private float getScore(Board board, byte color, Map<Byte, List<int[]>> pieceMap) {
		List<int[]> bishops = pieceMap.get((byte) (Piece.BISHOP|color));
		if(bishops == null) {
			return 0f;
		}
		
		byte oppColor = (color == Piece.WHITE ? Piece.BLACK : Piece.WHITE);
		
		float score = 0f;
		for(int[] pos: bishops) {
			for(int[] dirs: BishopMoveGenerator.DIRECTIONS) {
				if( ! Board.isOnBoard(pos[0]+dirs[0], pos[1]+dirs[1])) {
					score -= 0.05f;
					continue;
				}
				byte rPiece = board.getPiece(pos[0]+dirs[0], pos[1]+dirs[1]);
				if(rPiece == 0 || (rPiece&Piece.MASK_COLOR) == oppColor) {
					score += 0.1f;
				} else {
					score -= 0.1f;
				}
			}
		}
		return score;
	}
}
